%%------------------------------------------------------------------------------
% @doc kvcache_test module
% @author hgx <hgx@live.cn>
% @copyright 2024 hgx, All rights reserved.
% @since 2024-05-21 
%%------------------------------------------------------------------------------
-module(kvcache_test).

-include_lib("eunit/include/eunit.hrl").

-export([run/0]).

%% public functions
run() ->
    ?debugMsg("-- --  kvcache test begin -- --"),
    {ok, _} = application:ensure_all_started(kvcache),
    ?debugFmt("-- erlang system_time second: ~p", [erlang:system_time(second)]),
    Pid = erlang:whereis(kvcache_worker),

    ?debugMsg("-----------------------------------------------------------------"),
    ?debugMsg("-- -- Incorrect function call -- --"),
    ?debugFmt("-- kvcache:cache(undefined, 0), throw: ~p", [catch kvcache:cache(undefined, 0)]),
    ?debugFmt("-- kvcache:set_timeout(-1), throw: ~p", [catch kvcache:set_timeout(-1)]),
    
    ?debugMsg("-----------------------------------------------------------------"),
    ?debugMsg("-- -- init kvcache_worker data -- --"),
    kvcache:cache(fun fetch_test/1, 1),
    timer:sleep(1000),
    ?debugFmt("-- kvcache_worker process_info: ~p", [erlang:process_info(Pid)]),

    ?debugFmt("-- kvcache_worker get 1: ~p", [kvcache:cache(fun fetch_test/1, 1)]),
    ?debugFmt("-- kvcache_worker get 2: ~p", [kvcache:cache(fun fetch_test/1, 2)]),
    ?debugFmt("-- ets tab2list: ~p", [ets:tab2list(kvcache_worker)]),
    ?debugMsg("-----------------------------------------------------------------"),
    ?debugMsg("-- -- kvcache_worker set_timeout 0.001 hour -- --"),
    kvcache:set_timeout(0.001),
    ?debugFmt("-- kvcache_worker state: ~p", [gen_server:call(kvcache_worker, show)]),
    ?debugFmt("-- kvcache_worker get 1: ~p", [kvcache:cache(fun fetch_test/1, 1)]),
    ?debugFmt("-- kvcache_worker get 2: ~p", [kvcache:cache(fun fetch_test/1, 2)]),
    timer:sleep(1000),
    ?debugFmt("-- kvcache_worker process_info: ~p", [erlang:process_info(Pid, dictionary)]),
    ?debugMsg("-----------------------------------------------------------------"),
    ?debugMsg("-- -- kvcache_worker timecallback after, waiting 1 minute -- --"),
    timer:sleep(60_000),
    ?debugMsg("-- -- kvcache_worker timecallback befor -- --"),
    ?debugFmt("-- local timestamp: ~p", [erlang:system_time(second)]),
    ?debugFmt("-- kvcache_worker process_info: ~p", [erlang:process_info(Pid, dictionary)]),
    ?debugFmt("-- ets tab2list: ~p", [ets:tab2list(kvcache_worker)]),
    ?debugMsg("-----------------------------------------------------------------"),
    ?debugMsg("-- -- kvcache_worker clean_all befor -- --"),
    kvcache:cache(fun fetch_test/1, 1),
    kvcache:cache(fun fetch_test/1, 2),
    kvcache:cache(fun fetch_test/1, 3),
    timer:sleep(1000),
    ?debugFmt("-- ets tab2list: ~p", [ets:tab2list(kvcache_worker)]),
    ?debugFmt("-- kvcache_worker process_info: ~p", [erlang:process_info(Pid, dictionary)]),
    kvcache:clean_all(),
    ?debugMsg("-- -- kvcache_worker clean_all after -- --"),
    timer:sleep(1000),
    ?debugFmt("-- kvcache_worker process_info: ~p", [erlang:process_info(Pid, dictionary)]),
    ?debugFmt("-- ets tab2list: ~p", [ets:tab2list(kvcache_worker)]),

    ?debugFmt("-- erlang system_time second: ~p", [erlang:system_time(second)]),

    application:stop(kvcache),
    ?debugMsg("-----------------------------------------------------------------"),
    ?debugMsg("-- -- kvcache test end -- --"),
    ok.


%% internal functions
fetch_test(Key) ->
    integer_to_list(Key).

